import axios from "axios";
import qs from "qs";

let instance = axios.create({
  baseURL: "http://localhost:2345", //统一路径
  timeout: 5000,
});

instance.interceptors.request.use(
  (config) => {
    console.log(config);
    // config-- 前台向后台发送请求的信息
    if (config.method == "post") {
      config.data = qs.stringify({ ...config.data });
    } else {
      config.data = { ...config.data };
    }
    //加一个头部信息，告诉浏览器接收的是对象的数据类型
    config.headers["Content-Type"] = "application/X-www-form-urlencoded";

    return config;
  },
  //抛出错误处理
  (error) => {
    return Promise.reject(error);
  }
);

// 添加响应拦截器
instance.interceptors.response.use(
  (response) => {
    console.log(response.data.data);
    return response;
  },
  (error) => {
    return Promise.reject(error);
  }
);

export default instance;
